package com.ml.mall.service.country.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.country.CountryStatesAddDto;
import com.ml.mall.bean.req.country.CountryStatesPageDto;
import com.ml.mall.bean.req.country.GetIdDto;
import com.ml.mall.entity.country.CountryStates;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.country.CountryStatesMapper;
import com.ml.mall.service.country.CountryStatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CountryStatesServiceImpl extends ServiceImpl<CountryStatesMapper, CountryStates> implements CountryStatesService {
    @Autowired
    private CountryStatesMapper countryStatesMapper;

    //添加州名
    @Override
    public int addCountryStates(CountryStatesAddDto countryStatesAddDto){
        //根据信息判断是否在数据库表中已经存在该州
        QueryWrapper<CountryStates> checkIfHadStatesName = new QueryWrapper<>();
        checkIfHadStatesName.eq("country_id",countryStatesAddDto.getCountryId());
        checkIfHadStatesName.eq("states_name",countryStatesAddDto.getStatesName());
        CountryStates countryStates = countryStatesMapper.selectOne(checkIfHadStatesName);
        if (countryStates!=null){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
        //执行添加州名操作
        CountryStates insertCountryStates = new CountryStates();
        insertCountryStates.setCountryId(countryStatesAddDto.getCountryId());
        insertCountryStates.setStatesName(countryStatesAddDto.getStatesName());
        int insert = countryStatesMapper.insert(insertCountryStates);
        if (insert==0){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return insert;
    }

    //进行分页查询
    @Override
    public com.ml.mall.bean.page.Page<CountryStates> queryAllCountryStatesByPage(CountryStatesPageDto dto) {
        com.ml.mall.bean.page.Page<CountryStates> statesPageRes = new com.ml.mall.bean.page.Page<>(dto.getPageIndex(),dto.getPageNum());

        //执行分页查询
        QueryWrapper<CountryStates> pageWrapper = new QueryWrapper<>();
        pageWrapper.eq("country_id",dto.getCountryId());
        IPage<CountryStates> page = new Page<>(dto.getPageIndex(),dto.getPageNum());
        page = countryStatesMapper.selectPage(page, pageWrapper);

        statesPageRes.setTotal((int)page.getTotal());
        statesPageRes.setRecords(page.getRecords());
        return statesPageRes;
    }

    //批量删除操作
    @Override
    public int deleteStatesByBatchId(GetIdDto getIdDto){
        //数组转变成list集合，符合批量删除条件
        List<Long> list=Arrays.asList(getIdDto.getId());
        int del = countryStatesMapper.deleteBatchIds(list);
        if (del==0){
            throw new BizException(CommonEnum.DELETE_ERROR);
        }
        return del;
    }
}
